import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/***
 * 二分搜索树:存储的数据必须具有可比较性
 */
public class BST<E extends Comparable<E>> {
    private class Node {
        private E e;
        private Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }

    private Node root;
    private int size;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(E e) {
        root = add(root, e);
    }

    private Node add(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            //向左子树插入数据,并将新生成的NODE挂接成左子树
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            //向右子树插入数据,并将新生成的NODE挂接成右子树
            node.right = add(node.right, e);
        }
        return node;
    }

    //查询二分搜索树中是否包含有某元素e
    public boolean contains(E e) {
        return contains(root, e);

    }

    private boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            //找到了
            return true;
        }
        if (e.compareTo(node.e) < 0) {
            //在当前节点的左子树中继续查找
            return contains(node.left, e);
        } else {
            return contains(node.right, e);
        }
    }

    //二叉树的前序遍历
    public void preOrder() {
        preOrder(root);
    }

    //对根节点为node的二叉搜索树进行前序遍历
    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        //遍历左子树
        preOrder(node.left);
        //遍历右子树
        preOrder(node.right);
    }

    //二叉树的前序遍历,非递归算法
    public void preOrderNR() {
        Stack<Node> stack = new Stack<>();
        //先将根节点压入栈
        stack.push(root);
        while (!stack.isEmpty()) {
            //出栈一个元素,进行访问
            Node cur = stack.pop();
            System.out.println(cur.e);
            //将左右孩子压入栈,因为栈是先进后出,所以此处前序遍历应该先压入右孩子,再压入左孩子
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
    }

    //二叉树的中序遍历
    public void inOrder() {
        inOrder(root);

    }

    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    //二叉树的后序遍历
    public void postOrder() {
        postOrder(root);
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    //二叉搜索树的层序遍历(广度优先遍历)
    public void levelOrder() {
        Queue<Node> q = new LinkedList<>();
        while (!q.isEmpty()) {
            Node cur = q.remove();
            System.out.println(cur.e);

            if (cur.left != null) {
                q.add(cur.left);
            }
            if (cur.right != null) {
                q.add(cur.right);
            }
        }
    }

    //寻找二分搜索树的最小值所在节点
    public E mininum() {
        if (size == 0) {
            throw new IllegalArgumentException("mininum error,BST is empty");
        }
        return mininum(root).e;
    }

    public Node mininum(Node node) {
        if (node.left == null) {
            //找到了最小值
            return node;
        }
        return mininum(node.left);
    }

    //寻找二分搜索树的最大值所在节点
    public E maxinum() {
        if (size == 0) {
            throw new IllegalArgumentException("mininum error,BST is empty");
        }
        return maxinum(root).e;
    }

    public Node maxinum(Node node) {
        if (node.right == null) {
            //找到了最大值
            return node;
        }
        return mininum(node.right);
    }

    //删除二叉搜索树中的最小值所在的节点,并返回最小值
    public E removeMin() {
        //获取最小值
        E ret = mininum();
        root = removeMin(root);
        return ret;
    }

    public Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    //删除二叉搜索树中的最大值所在的节点,并返回最大值
    public E removeMax() {
        //获取最小值
        E ret = maxinum();
        root = removeMax(root);
        return ret;
    }

    public Node removeMax(Node node) {
        if (node.right == null) {
            Node leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    //删除二分搜索树中的任意值的节点
    public void remove(E e){
        root = remove(root,e);
    }
    private Node remove(Node node,E e){
        if (node == null) {
            return null;
        }
        if(e.compareTo(node.e) < 0){
            //在node的左子树中递归调用
            node.left = remove(node.left,e);
            return node;
        }else if(e.compareTo(node.e) > 0){
            //在node的右子树中递归调用
            node.right = remove(node.right,e);
            return node;
        }else{//此节点就是需要删除的节点
            if(node.left == null){
                Node rNode = node.right;
                node.right = null;
                size --;
                return rNode;
            }
            if(node.right == null){
                Node lNode = node.left;
                node.left = null;
                size -- ;
                return lNode;

            }
            //左右子树均不为空
            //找到后继结点,此处找被删除节点右子树的最小值为后继结点
            Node successor = mininum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            node.left = node.right = null;
            return successor;
        }
    }
}